package ext.vpde.cadvaulting.util;

import wt.epm.*;
import wt.epm.structure.*;
import wt.fc.*;
import wt.query.*;

public class ReleaseDrawing {
	
	public static void findRelatedDocuments(EPMDocument drawing) throws Exception {
		System.out.println("SEARCHING FOR DOCUMENTS RELATED TO " + drawing.getCADName());
		QueryResult qr = EPMStructureHelper.service.navigateReferences(drawing, null, true);
		while (qr.hasMoreElements()) {
			EPMDocumentMaster epmdoc = (EPMDocumentMaster) qr.nextElement();
			System.out.println("FOUND MASTER: " + epmdoc.getCADName());
			QuerySpec qs   = new QuerySpec(EPMDocument.class);
			qs.appendWhere(new SearchCondition(EPMDocument.class,"master>CADName",  "=", epmdoc.getCADName()));
			qs.appendOrderBy(EPMDocument.class,"thePersistInfo.theObjectIdentifier.id",true);
			QueryResult qi = PersistenceHelper.manager.find(qs);
			while (qi.hasMoreElements()) {
				EPMDocument thisDoc = (EPMDocument)qi.nextElement();
				if (thisDoc.isLatestIteration()) {
					System.out.println("FOUND LATEST ITERATION: " + thisDoc.getCADName());
					ReleaseDrawing.changeState(thisDoc);
				}
			}				
		}
	}
	
	public static void changeState(EPMDocument doc) {
		System.out.println("STATE: " + doc.getState());
		
	}
}
